package com.liuzh.kaoyanspringboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.liuzh.kaoyanspringboot.common.Result;
import com.liuzh.kaoyanspringboot.entity.Article;
import com.liuzh.kaoyanspringboot.entity.Files;
import com.liuzh.kaoyanspringboot.entity.User;
import com.liuzh.kaoyanspringboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private LikeService likeService;
    @GetMapping ("/example")
    public Result get() {
        Map<String, Object> map =new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
        return Result.success(map);
    }
    @GetMapping("/members")
    public Result members() {
        Map<String, Object> map = new HashMap<>();
        List<User> list = userService.list();
        List<Article> articleList = articleService.listDate();
        int uq[] = {0,0,0,0};
        int aq[] = {0,0,0,0};
        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1: uq[0] +=1; break;
                case Q2: uq[1] +=1; break;
                case Q3: uq[2] +=1; break;
                case Q4: uq[3] +=1; break;
                default:break;
            }
        }
        for (Article article : articleList) {
            Date createTime = article.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1: aq[0] +=1; break;
                case Q2: aq[1] +=1; break;
                case Q3: aq[2] +=1; break;
                case Q4: aq[3] +=1; break;
                default:break;
            }
        }
        Integer userNum = Math.toIntExact(userService.count());
        Integer articleNum = Math.toIntExact(articleService.myCount());
        Integer commentNum = Math.toIntExact(commentService.myCount());
        Integer likeNum = Math.toIntExact(likeService.myCount());
        map.put("user",uq);
        map.put("article",aq);
        map.put("userNum",userNum);
        map.put("articleNum",articleNum);
        map.put("commentNum",commentNum);
        map.put("likeNum",likeNum);
        return Result.success(map);
    }
}
